package com.group.dao;

import java.util.Date;
import java.util.List;

import com.base.view.GeneralResult;
import com.group.model.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

@Repository
public interface BookDao {

	public List<Book> queryBook(@Param("name") String name, @Param("start") Integer start, @Param("limit") Integer limit,@Param("type") Integer type);

	public int queryBookCount(@Param("name") String name,@Param("type") Integer type);

	@Update("update book b set b.investmentQuota = #{arg0},b.profit = #{arg1} where b.id = #{arg2} ")
	public void updateBook(int investmentQuota, double profit,Long bookId);

	@Update("update book b set b.state = #{arg1} where b.id = #{arg0} ")
	public void updateBookState(Long bookId, int state);

	@Select("select ifnull(sum(l.cost),0) from bookreadlog l where l.date = #{age0} and l.userId is not null")
//	@Select("select ifnull(sum(l.cost),0) from bookreadlog l where l.date = #{age0}")
	public double countCost(String date);

	@Select("select  l.userId , sum(l.cost) totalCost from bookreadlog l where l.date = #{age0} and l.userId is not null group by l.userId")
//@Select("select  l.userId , sum(l.cost) totalCost from bookreadlog l where l.date = #{age0} group by l.userId")
	public List<ReadLogView> queryReadLogView(String date);

	@Update("update bookreadlog l set l.userId = #{arg1} where l.readerId = #{arg0}")
	public void updateReadLogUser(String readerId, Long userId);

	@Select("select * from UserTickerLog l where l.reason = '看书领币' and l.createTime >= #{arg0} order by num desc limit 20 ")
	public List<UserTickerLog> queryBonusRank(Date date);

	@Select("select ifnull(sum(l.cost),0) from bookreadlog l where l.date = #{arg0} and l.name = #{arg1}")
	public double countBookCost(String date, String name);

	@Select("select  ifnull(sum(l.amount),0) from bookinvestlog l where l.bookId = #{arg0} and l.endTime > date_add(now(), interval - 1 day)")
	public double countBookTotalInvest(Long bookId);

	@Select("select  ifnull(sum(l.amount),0) from bookinvestlog l where l.bookId = #{arg0} and l.createTime >= #{arg1} and l.createTime <= #{arg2} and l.endTime > date_add(now(), interval - 1 day) ")
	public double countBookyesterdayTotalInvest(Long bookId,Date date,Date date1);

	@Select("select * from bookinvestlog l where l.endTime > date_add(now(), interval - 1 day)")
	public List<BookInvestLog> queryAllBookInvestLogByBookId();

	@Select("SELECT ifnull(sum(b.bonus),0) bonus,userId FROM fingo.bookbonuslog b  where createTime >= #{arg0} group by userId")
	public List<BookBonusLog> queryAllBookBonusLogByBookId(String date);

	@Select("select * from userticker where userId in (SELECT userId FROM fingo.bookbonuslog b  where createTime >= #{arg0}) order by userId")
	public List<UserTicker> queryUserticker(String date);

	@Select("select * from book l where l.id = #{arg0}")
	public Book queryBookByBookId(Long bookId);

	@Select("select * from bookinvestlog l where l.bookId = #{arg0} and l.endTime > date_add(now(), interval - 1 day)")
	public List<BookInvestLog> queryBookInvestLogByBookId(Long bookId);

	@Select("select * from bookinvestlog l where l.state = 0 and l.endTime < now()")
	public List<BookInvestLog> queryExpireBookInvestLog();

	public List<BookInvestLog> queryBookInvestLog(@Param("userId") Long userId, @Param("start") Integer start, @Param("limit") Integer limit,@Param("date") String date,@Param("type") Integer type);

	public List<BookInvestLog> queryBookInvestLogDetail(@Param("userId") Long userId, @Param("start") Integer start, @Param("limit") Integer limit,@Param("userName") String userName,@Param("date") String date,@Param("type") Integer type);

	public int queryBookInvestLogCount(@Param("userId") Long userId,@Param("date") String date,@Param("type") Integer type);

	public Integer queryBookInvestAmountCount(@Param("userId") Long userId,@Param("date") String date,@Param("type") Integer type);

	public int queryBookInvestLogCounts(@Param("userId") Long userId,@Param("userName") String userName,@Param("date") String date,@Param("type") Integer type);

	@Select("select ifnull(sum(l.bonus),0) from bookbonuslog l left join book k on l.bookId = k.id where l.createTime >= #{arg1} and l.userId = #{arg0} and k.type = #{arg2}")
	public double queryYestodayBonus(Long userId, String date,Integer type);

	@Select("select ifnull(sum(l.bonus),0) from bookbonuslog l left join book k on l.bookId = k.id where l.userId = #{arg0} and k.type = #{arg1}")
	public double queryTotalBonus(Long userId,Integer type);
}
